//
//  Profile.m
//  Opper
//
//  Created by Le Dung on 4/12/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "Profile.h"
#define xLabel 26
#define yLabel 150
@interface Profile ()

@end
@implementation Profile
@synthesize containView;
@synthesize isMove;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{

    [StyleClass barButtonItemEdit:self left:YES right:NO];
    
}
-(void)rightPress
{

}

-(void)leftPress
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)acceptPress
{
    Member *member =[[Member alloc ] init ];
    member.firstName=[(UITextField *)[self.view viewWithTag:200] text];
    member.midleName=[(UITextField *)[self.view viewWithTag:201] text];
    member.lastname=[(UITextField *)[self.view viewWithTag:202] text];
    member.gender=@"1";
    member.address=[(UITextField *)[self.view viewWithTag:203] text];
    member.houseNumber=[(UITextField *)[self.view viewWithTag:204] text];
    member.postCode=[(UITextField *)[self.view viewWithTag:205] text];
    member.place=[(UITextField *)[self.view viewWithTag:206] text];
    member.phoneNumber=[(UITextField *)[self.view viewWithTag:207] text];
    member.birthDay=[(UITextField *)[self.view viewWithTag:208] text];
    member.firstName=[(UITextField *)[self.view viewWithTag:210] text];
    
    NSString *functionName =@"addMember";
    
    
    
    NSMutableDictionary *dictionary =[[[NSMutableDictionary alloc]init] autorelease];
//    [dictionary setValue:[GetData App].userAccount.userName forKey:@"email"];
//    [dictionary  setValue:[GetData App].userAccount.password forKey:@"password"];
    [dictionary setValue:[GetData App].userAccount.userName forKey:@"username"];
    [dictionary  setValue:[GetData App].userAccount.password forKey:@"passwd"];
    
        
    [dictionary setValue:member.firstName forKey:@"first_name"];
    [dictionary  setValue:member.lastname forKey:@"last_name"];
    [dictionary setValue:member.midleName forKey:@"insertion"];
    [dictionary  setValue:member.gender forKey:@"gender"];
    [dictionary setValue:member.address forKey:@"address"];
    [dictionary  setValue:member.houseNumber forKey:@"house_number"];
    [dictionary setValue:member.postCode forKey:@"postcode"];
    [dictionary setValue:member.place forKey:@"place"];
    [dictionary  setValue:@"mail" forKey:@"email"];
    [dictionary setValue:member.phoneNumber forKey:@"phone"];
    [dictionary  setValue:member.birthDay forKey:@"birthday"];

    
    NSString *jsonRespones =[GetData sendRequest:functionName andDictionary:dictionary];
    
    
    
//    username,passwd,first_name,last_name,insertion,gender,address,house_number,postcode,place,email,phone,birthday
    
    
    
    if(jsonRespones)
    {
        if(![GetData checkError:jsonRespones])
        {
        }
        else
        {
            NSString *massage =[GetData checkMassage:jsonRespones];
            UIAlertView *alerView =[[UIAlertView alloc]
                                    initWithTitle:@"Error" message:massage delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil ];
            [alerView show];
            [alerView release];
        }
        
    }

    
    
    
}
-(void)cancelPress
{
    [self.navigationController  popViewControllerAnimated:YES];
}


-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
}
-(void)buttonTarget
{
    [self.acceptButton addTarget:self action:@selector(acceptPress) forControlEvents:UIControlEventTouchUpInside];
    [self.acceptButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    
    
    [self.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelButton setBackgroundImage:[StyleClass nRed] forState:UIControlStateNormal];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buttonTarget];
    [StyleClass background:self.view];
    [StyleClass createNavi:self andTitle:@"Profile"];

    
    [self style];
    [self createContent];
}
-(void)style
{
    self.containView.userInteractionEnabled =YES;
    [self.containView sizeToFit];
    self.containView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.containView.layer.shadowOffset = CGSizeMake(0.0, 3.0);
    self.containView.layer.shadowOpacity = 1.0f;
}


///100 < button tag < 150;
-(void)createContent
{
    self.containView.backgroundColor=[UIColor whiteColor];
    
    UILabel *gender =[[UILabel alloc]initWithFrame:CGRectMake(8, 0, 140, 32)];

    gender.text=@"Hr/Mevr";
    [self.containView addSubview:gender];
    [gender release];
    
    UIButton *genderBtA=[UIButton buttonWithType:UIButtonTypeCustom];
    [genderBtA setBackgroundImage:[UIImage imageNamed:@"Boy.png"] forState:UIControlStateNormal];
    genderBtA.frame =CGRectMake(160, 3, 25, 25);
    genderBtA.tag =101;
    [genderBtA addTarget:self action:@selector(genderSelect:) forControlEvents:UIControlEventTouchUpInside];
    [genderBtA setAlpha:0.5];
    [self.containView addSubview:genderBtA];
    
    UIButton *genderBtB=[UIButton buttonWithType:UIButtonTypeCustom];
    [genderBtB setBackgroundImage:[UIImage imageNamed:@"Girl.png"] forState:UIControlStateNormal];
    genderBtB.frame =CGRectMake(195, 3, 25, 25);
    genderBtB.tag =102;
    [genderBtB addTarget:self action:@selector(genderSelect:) forControlEvents:UIControlEventTouchUpInside];
    [genderBtB setAlpha:0.5];
    [self.containView addSubview:genderBtB];

    NSArray *labelTitel=[NSArray arrayWithObjects:@"Voornaan",@"Tussenvoegse",@"Achternaam",@"Adres",@"Huisnummer",@"Post code",@"Plaats",@"Telefoon nr",@"Geboorte date",@"Wachtwoord",@"Herhaal wachtwoord",nil];
    
    for(int i = 0 ; i<11; i++)
    {
        int yLocation= 33+i*27;
        
        UILabel *unit =[[UILabel alloc]initWithFrame:CGRectMake(8, yLocation, 140, xLabel)];
        unit.text=[labelTitel objectAtIndex:i];
        [self labelFormat:unit];
        [self.containView addSubview:unit];
        [unit release];
        
        UIImageView *line=[[UIImageView alloc]initWithFrame:CGRectMake(0, 33+i*27, 150, 1)];
        line.backgroundColor =[UIColor brownColor];
        [self.containView addSubview:line];
        [line release];
        
        UITextField *tUnit =[[UITextField alloc]initWithFrame:CGRectMake(160, yLocation+1, 140, xLabel+2)];
        tUnit.backgroundColor=[UIColor whiteColor];
        tUnit.delegate=self;
        tUnit.tag=200+i;
        tUnit.placeholder =[NSString stringWithFormat:@"%d",i];
        [tUnit resignFirstResponder];
        
        [self.containView addSubview:tUnit];
        [tUnit release];
        
        UIImageView *line2=[[UIImageView alloc]initWithFrame:CGRectMake(160, 33+i*27, 140, 1)];
        line2.backgroundColor =[UIColor brownColor];
        [self.containView addSubview:line2];
        [line2 release];

        
    }
}

-(IBAction)genderSelect:(id)sender
{
    UIButton *button =(UIButton *)sender;
    
    if(button.tag==101)
    {
        [button setAlpha:1];
        self.isGender =1;
        UIButton *buttonAl =(UIButton *)[self.view viewWithTag:102];
        [buttonAl setAlpha:0.5];
    }
    if(button.tag==102)
    {
        self.isGender =0;
        NSLog(@"sdf");
        [button setAlpha:1];
        UIButton *buttonAl =(UIButton *)[self.view viewWithTag:101];
        [buttonAl setAlpha:0.5];
    }
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    switch (textField.tag) {
        case 204:
            [textField setKeyboardType:UIKeyboardTypeNumberPad];
            break;
            
        default:
            break;
    }
    
    
    if(!isMove &&textField.frame.origin.y>140)
    {
        [StyleClass moveUp:self.containView andYLocation:160];
        isMove=1;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(isMove)
    {
        [StyleClass moveDown:self.containView andYLocation:160];
        isMove=0;
    }
    
    [textField resignFirstResponder];
    return YES;
}

-(void)labelFormat:(UILabel*)label
{

    label.font = [UIFont systemFontOfSize:15];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [containView release];
    [_acceptButton release];
    [_cancelButton release];
    [super dealloc];
}
@end
